package com.inlycat.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.inlycat.entity.Thumb;
import com.inlycat.entity.Tramp;
import com.inlycat.entity.VideoAttr;
import com.inlycat.mapper.ThumbMapper;
import com.inlycat.mapper.TrampMapper;
import com.inlycat.mapper.VideoAttrMapper;
import com.inlycat.service.TrampService;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月24日下午2:16:30
 * @category
 **/
@Service("trampService")
public class TrampServiceImpl implements TrampService {

	@Resource
	private TrampMapper trampMapper;
	
	@Resource
	private ThumbMapper thumbMapper;

	@Resource
	private VideoAttrMapper videoAttrMapper;

	@Override
	public boolean insert(Tramp tramp) {
		boolean flag = false;
		/**
		 * 点踩之前取消赞
		 */
		Thumb thumb = new Thumb(tramp.getUserId(),tramp.getVideoId());
		if(thumbMapper.hasThumb(thumb)>0){
			flag = thumbMapper.delete(thumb);
		}
		boolean result = trampMapper.insert(tramp);
		if (result) {
			VideoAttr videoAttr = new VideoAttr();
			videoAttr.setVideoId(tramp.getVideoId());
			if(flag)
				videoAttr.setThumb(-1);
			videoAttr.setTramp(1);
			result = videoAttrMapper.updateSelectiveByVideoId(videoAttr);
		}
		return result;
	}

	@Override
	public boolean delete(Tramp tramp) {
		boolean result = trampMapper.delete(tramp);
		if (result) {
			VideoAttr videoAttr = new VideoAttr();
			videoAttr.setVideoId(tramp.getVideoId());
			videoAttr.setTramp(-1);
			result = videoAttrMapper.updateSelectiveByVideoId(videoAttr);
		}
		return result;
	}

	@Override
	public boolean hasTramp(Tramp tramp) {
		boolean result = trampMapper.hasTramp(tramp) > 0 ? true : false;
		return result;
	}

}
